package controllers

import (
	"log"
	"xmgl/models"

	//"log"

	"github.com/astaxie/beego"
)

/*
直播/微信直播
*/
type LiveController struct {
	beego.Controller
}

type WxLiveController struct {
	beego.Controller
}

type Api_liveController struct {
	beego.Controller
}

func init() {

}

func (this *LiveController) Get() {
	var (
		user models.Sut_user
		//qrcode models.Sut_qrcode
	)
	if this.GetSession("USER") == nil {
		//没有取到session值，页面跳转
		//http.Redirect(this.Ctx.ResponseWriter, this.Ctx.Request, "/login", http.StatusFound)
	}
	tmp, ok := this.GetSession("USER").(models.Sut_user) //注意类型断言
	if ok {
		user = tmp
	}

	//log.Println("从session取用户名：", tmp.User_name, "ID：", tmp.User_id)
	//sess := this.StartSession()
	//sess.Set("USER", user)
	//log.Println("从session取用户名：", sess.Get("USER"))

	this.Data["WEBSITE"] = "直播/直播"
	this.Data["USER_NAME"] = user.User_name
	//qrcode.GetQrcode("http://c.kjdll.com:8888/hls/test.m3u8")
	//this.Data["SJEWM"] = string(qrcode.Qrcode[:])
	this.TplName = "live.html"

}

func (this *WxLiveController) Get() {
	var (
		user models.Sut_user
		//qrcode models.Sut_qrcode
	)
	if this.GetSession("USER") == nil {
		//没有取到session值，页面跳转
		//http.Redirect(this.Ctx.ResponseWriter, this.Ctx.Request, "/login", http.StatusFound)
	}
	tmp, ok := this.GetSession("USER").(models.Sut_user) //注意类型断言
	if ok {
		user = tmp
	}

	//log.Println("从session取用户名：", tmp.User_name, "ID：", tmp.User_id)
	//sess := this.StartSession()
	//sess.Set("USER", user)
	//log.Println("从session取用户名：", sess.Get("USER"))

	this.Data["WEBSITE"] = "直播/直播"
	this.Data["USER_NAME"] = user.User_name
	//qrcode.GetQrcode("http://c.kjdll.com:8888/hls/test.m3u8")
	//this.Data["SJEWM"] = string(qrcode.Qrcode[:])
	this.TplName = "live_wx.html"

}

func (this *Api_liveController) Get() {
	//取服务器状态 /api/v1/live/getServerStatus
	//取得直播的SOURCE地址	/api/v1/live/getLiveSources
	var (
		command string
		err     error
	)
	command = this.GetString(":command", "")
	if command == "" {
		this.Data["json"] = err
		this.ServeJSON()
	}
	if command == "getServerStatus" {
		//http://127.0.0.1/api/v1/live/getServerStatus
		var r *models.Sut_RTMP
		r = &models.Sut_RTMP{}
		r.GetServerState()
		this.Data["json"] = r
		this.ServeJSON()
		log.Println("func (this *Api_liveController) Get() {} \t uptime：", r.Uptime)
	} else if command == "getLiveSources" {
		//http://127.0.0.1/api/v1/live/getLiveSources
		var (
			s models.Sut_live
		)
		err = s.Get_Live_Src()
		if err != nil {
			this.Data["json"] = err
			this.ServeJSON()
		}
		this.Data["json"] = s.Sources
		this.ServeJSON()
	} else if command == "getAliyunLiveRtmpSources" {
		//http://127.0.0.1/api/v1/live/getAliyunLiveRtmpSources
		var s models.Stu_Aliyun_Src
		var retu models.Sut_Ajax_Retu_Data
		retu.Data, err = s.GetLiveSrc("aliyun/rtmp")
		if err != nil {
			retu.StatusCode = models.StatusCodeErr
			this.Data["json"] = retu
			this.ServeJSON()
		} else {
			retu.StatusCode = models.StatusCodeOk
			this.Data["json"] = retu
			this.ServeJSON()
		}
	} else if command == "DescribeLiveStreamOnlineUserNum" {
		var s models.Stu_Aliyun_ZhiBo
		s.Init()
		err = s.Get1("l.kjdll.com")
		//err = s.GetOnlineUserNum1("l.kjdll.com")
		var retu models.Sut_Ajax_Retu_Data
		if err != nil {
			retu.StatusCode = models.StatusCodeErr
			retu.Data = s.LiveStreamOnlineUserNum.TotalUserNumber
			this.Data["json"] = retu
			this.ServeJSON()
		} else {
			retu.StatusCode = models.StatusCodeOk
			retu.Data = s.LiveStreamOnlineUserNum.TotalUserNumber
			this.Data["json"] = retu
			this.ServeJSON()
		}
	}

}
